home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / zip10ex.zip / TEMPF.C < prev    next >
C/C++ Source or Header  |  1991-10-03  |  4KB  |  208 lines

  1. /*
  2.  
  3.  Copyright (C) 1990,1991 Mark Adler, Richard B. Wales, and Jean-loup Gailly.
  4.  Permission is granted to any individual or institution to use, copy, or
  5.  redistribute this software so long as all of the original files are included
  6.  unmodified, that it is not sold for profit, and that this copyright notice
  7.  is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  tempf.c by Mark Adler.
  13.  */
  14.  
  15. #include "tailor.h"
  16. #include "tempf.h"
  17.  
  18. extern char *tempname OF((int));
  19.  
  20. #ifdef MODERN
  21. #  include <string.h>
  22. #else /* !MODERN */
  23.    voidp *malloc();
  24. #  ifndef ZMEM
  25.      char *memcpy();
  26. #  endif /* !ZMEM */
  27. #endif /* ?MODERN */
  28. #ifdef ZMEM
  29.    char *memcpy OF((char *, char *, unsigned int));
  30. #endif /* ZMEM */
  31. int unlink OF((char *));
  32.  
  33.  
  34. /* Define a special memcpy for MSDOS small model */
  35. #if defined(MSDOS) && (defined(M_I86SM) || defined(__SMALL__))
  36. #  include <dos.h>
  37. #  define memcpy farmemcpy
  38.    void farmemcpy(char far *d, char far *s, unsigned n)
  39.    {
  40.       movedata(FP_SEG(s), FP_OFF(s), FP_SEG(d), FP_OFF(d), n);
  41.    }
  42. #endif
  43.  
  44.  
  45. tFILE *topen(c)
  46. int c;
  47. /* Create a new temporary file and return its descriptor.  Save the character
  48.    c to be used in the temporary file name, if needed. */
  49. {
  50.   tFILE *t;
  51.  
  52.   if ((t = (tFILE *)malloc(sizeof(tFILE))) == NULL ||
  53.       (t->b = farmalloc(TMPSIZ)) == NULL)
  54.     return NULL;
  55.   t->p = t->m = 0;
  56.   t->c = c;
  57.   t->f = NULL;
  58.   return t;
  59. }
  60.  
  61.  
  62. int tnew(t)
  63. tFILE *t;               /* temporary file descriptor */
  64. /* Create a temporary file with a unique name */
  65. {
  66.   return (t->n = tempname(t->c)) == NULL ||
  67.          (t->f = fopen(t->n, FOPW)) == NULL;
  68. }
  69.  
  70.  
  71. unsigned twrite(b, s, n, t)
  72. char *b;                /* buffer to write from */
  73. unsigned s;             /* size of items */
  74. unsigned n;             /* number of items */
  75. tFILE *t;               /* temporary file descriptor */
  76. /* Like fwrite()--will create a temporary file if needed. */
  77. {
  78.   unsigned j;           /* room in memory, items to write */
  79.   long k;               /* bytes to write */
  80.   long w;               /* bytes written to file */
  81.  
  82.   /* write to memory portion */
  83.   j = TMPSIZ - t->p;
  84.   k = s * (long) n;
  85.   if (j && k)
  86.   {
  87.     j = (long)j > k ? (unsigned)k : j;
  88.     memcpy(t->b + t->p, (char far *)b, j);
  89.     t->p += j;
  90.     if (t->m < t->p)
  91.       t->m = t->p;
  92.     b += j;
  93.     k -= j;
  94.   }
  95.   if (k == 0)
  96.     return n;
  97.  
  98.   /* create temporary file if needed */
  99.   if (t->f == NULL && tnew(t))
  100.     return 0;
  101.  
  102.   /* write to temporary file */
  103.   j = (unsigned)(k / s);
  104.   if (j && fwrite(b, s, j, t->f) != j)
  105.     return 0;
  106.   b += w = s * (long)j;
  107.   k -= w;
  108.   if (k && fwrite(b, (unsigned)k, 1, t->f) != 1)
  109.     return 0;
  110.   return n;
  111. }
  112.  
  113.  
  114. int tflush(t)
  115. tFILE *t;               /* temporary file descriptor */
  116. /* Like fflush() */
  117. {
  118.   return t->f == NULL ? 0 : fflush(t->f);
  119. }
  120.  
  121.  
  122. void trewind(t)
  123. tFILE *t;               /* temporary file descriptor */
  124. /* Like rewind() */
  125. {
  126.   t->p = 0;
  127.   if (t->f != NULL)
  128.     rewind(t->f);
  129. }
  130.  
  131.  
  132. unsigned tread(b, s, n, t)
  133. char *b;                /* buffer to read into */
  134. unsigned s;             /* size of items */
  135. unsigned n;             /* number of items */
  136. tFILE *t;               /* temporary file descriptor */
  137. /* Like fread() */
  138. {
  139.   unsigned j;           /* bytes in memory, items to read */
  140.   long k;               /* bytes requested */
  141.   long r;               /* bytes read from file */
  142.  
  143.   /* read from memory */
  144.   j = t->m - t->p;
  145.   k = s * (long)n;
  146.   if (j && k)
  147.   {
  148.     j = (long)j > k ? (unsigned)k : j;
  149.     memcpy((char far *)b, t->b + t->p, j);
  150.     t->p += j;
  151.     b += j;
  152.     k -= j;
  153.   }
  154.  
  155.   /* read from file if more requested */
  156.   if (k && t->f != NULL)
  157.   {
  158.     j = (unsigned)(k / s);
  159.     if (j)
  160.     {
  161.       r = s * (long)fread(b, s, j, t->f);
  162.       b += r;
  163.       k -= r;
  164.     }
  165.     if (k && k < s)
  166.       k -= fread(b, 1, (unsigned)k, t->f);
  167.   }
  168.  
  169.   /* return complete items read */
  170.   return n - (unsigned)((k + s - 1) / s);
  171. }
  172.  
  173.  
  174. int terror(t)
  175. tFILE *t;               /* temporary file descriptor */
  176. /* Like ferror() */
  177. {
  178.   return t->f == NULL ? 0 : ferror(t->f);
  179. }
  180.  
  181.  
  182. int teof(t)
  183. tFILE *t;               /* temporary file descriptor */
  184. /* Like feof() */
  185. {
  186.   return t->f == NULL ? t->p == t->m : feof(t->f);
  187. }
  188.  
  189.  
  190. int tclose(t)
  191. tFILE *t;               /* temporary file descriptor */
  192. /* Like fclose()--frees the memory used by the descriptor and deletes
  193.    the temporary file, if any. */
  194. {
  195.   int r;
  196.  
  197.   r = 0;
  198.   if (t->f != NULL)
  199.   {
  200.     r = fclose(t->f);
  201.     unlink(t->n);
  202.     free(t->n);
  203.   }
  204.   farfree(t->b);
  205.   free(t);
  206.   return r;
  207. }
  208.